<?php

namespace App\Http\Controllers\Frontend;

use App\Logics\NewsLogic;
use App\Logics\PushLogic;
use App\Logics\SiteLogic;


class PushController extends BaseController
{
    // 设置邮件推送 初始化
    public function initialize()
    {
        parent::initialize();
    }


    // 首页
    // https://api.asiatechwire.com/push.html
    public function index()
    {
        // 数据
        $info   = $this->getCateInfo();
        $cateId = $info['id'] ?? 0;
        $html   = $this->getLists($cateId);
        // 下一页
        $nextPage = $this->nextPage('push', $info);

        // 传值
        $option = $this->optLogic();
        $option->assign('nextPage', $nextPage);
        $option->assign('html', $html);

        return $this->dispaly('frontend.push.index');
    }


    // 获取更多
    public function more()
    {
        // 数据
        $info   = $this->getCateInfo();
        $cateId = $info['id'] ?? 0;
        $html   = $this->getLists($cateId);
        // 下一页
        $nextPage = $this->nextPage('push', $info);

        return $this->success(['html' => (string)$html, 'nextUrl' => $nextPage]);
    }

    // 设置邮件
    public function toEmail()
    {
        $logic = new PushLogic();
        $res   = $logic->toEmail();
        if($res === false){
            return $this->error($logic->getError());
        }

        return $this->success([], $res == 1 ? '设置推送成功！' : '取消推送成功！');
    }


    // 获取数据
    protected function getLists($cateId = 0)
    {
        $option = $this->optLogic();
        $option->setField('cate_id', $cateId);
        $option->setField('rows', 15);
        $option->setField('is_count', null);  //
        $option->setField('is_first', 1);  // 返回详情第一段
        $option->setField('title', null);  // 避免页面传值的时候进行搜索

        $logic  = new NewsLogic();
        $result = $logic->onLists($this->myUid);

        // 结果整理
        $result['nList']      = $this->makeNewsLists($result['list']);
        $result['style_host'] = env('STYLE_HOST');
        $result['app_host']   = request()->getSchemeAndHttpHost().'/';

        return $this->dispaly('frontend.push.on-list', $result);
    }


    // 获取栏目id
    protected function getCateInfo()
    {
        $option = $this->optLogic();
        $title  = $option->getData('title', null, 'trim');
        $page   = $option->getData('page', 1, 'intval');

        // 栏目id
        $info = [];
        if($title){
            $title = str_replace('-', ' ', $title);
            $lists = (new SiteLogic())->cates();
            foreach($lists as $item){
                if($item['en_title'] == $title){
                    $info = $item;
                    break;
                }
            }
        }

        return $info;
    }

    // 按日期整理
    protected function makeNewsLists($lists)
    {
        $nList = [];
        foreach($lists as $_row){
            $date = date('Y.m.d', $_row['ctime']);
            // 整理
            $_row['date'] = $date;
            // 整理
            $nList[$date][] = $_row;
        }

        return $nList;
    }

    // 接口处理失败时调用 404页面
    protected function error(string $message = '操作失败', array $data = [], int $code = ERROR_CODE)
    {
        $msg  = $this->optLogic()->translate($message);
        $data = empty($data) ? [] : $data;
        $json = ['msg' => $msg, 'code' => $code, 'data' => $data,];

        return $json;
    }
}


